package com.zy.ivideo.controller;

import com.zy.ivideo.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller("web_Scoket_system")
@RequestMapping("/api/socket")
@Api(value = "WebSocket接口", description = "该模块提供WebSocket模块服务", tags = "WebSocket模块")
public class WebSocketController {
    //页面请求
    @GetMapping("/index/{userId}")
    public ModelAndView socket(@PathVariable String userId) {
        ModelAndView mav = new ModelAndView("/socket1");
        mav.addObject("userId", userId);
        return mav;
    }

    //推送数据接口
    @ResponseBody
    @ApiOperation(value = "推送数据接口", notes = "推送数据接口", httpMethod = "POST")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "cid", value = "客户端id", required = true),
            @ApiImplicitParam(dataType = "String", name = "message", value = "消息", required = true),
    })
    @PostMapping("/socket/push/{cid}")
    public Map pushToWeb(@PathVariable String cid, String message) {
        Map<String,Object> result = new HashMap<>();
        try {
            WebSocketServer.sendInfo(message, cid);
            result.put("code", cid);
            result.put("msg", message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
